using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using UnityEngine;

public class LicenseVerificationController : MonoBehaviour
{
	[StructLayout(LayoutKind.Sequential, Size = 1)]
	private struct PackageInfo
	{
		internal string PackageName { get; set; }

		internal string SignatureHash { get; set; }
	}

	[Flags]
	private enum PackageInfoFlags
	{
		GetSignatures = 0x40
	}

	public string intendedSignatureHash;

	private void Start()
	{
		if (Application.platform == RuntimePlatform.Android)
		{
			CheckActualPackageName();
			CheckActualSignature(GetPackageInfo().SignatureHash);
			UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		}
	}

	private PackageInfo GetPackageInfo()
	{
		PackageInfo packageInfo = default(PackageInfo);
		packageInfo.PackageName = string.Empty;
		packageInfo.SignatureHash = string.Empty;
		PackageInfo result = packageInfo;
		try
		{
			AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
			AndroidJavaObject @static = androidJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
			if (@static == null)
			{
				BCWDebug.LogWarning("activity == null");
				return result;
			}
			result.PackageName = @static.Call<string>("getPackageName", new object[0]) ?? string.Empty;
			AndroidJavaObject androidJavaObject = @static.Call<AndroidJavaObject>("getPackageManager", new object[0]);
			if (androidJavaObject == null)
			{
				BCWDebug.LogWarning("manager == null");
				return result;
			}
			AndroidJavaObject androidJavaObject2 = androidJavaObject.Call<AndroidJavaObject>("getPackageInfo", new object[2] { result.PackageName, 64 });
			if (androidJavaObject2 == null)
			{
				BCWDebug.LogWarning("packageInfo == null");
				return result;
			}
			AndroidJavaObject[] array = androidJavaObject2.Get<AndroidJavaObject[]>("signatures");
			if (array == null)
			{
				BCWDebug.LogWarning("signatures == null");
				return result;
			}
			if (array.Length != 1)
			{
				BCWDebug.LogWarning("signatures.Length == " + array.Length);
				return result;
			}
			AndroidJavaObject androidJavaObject3 = array[0];
			byte[] buffer = androidJavaObject3.Call<byte[]>("toByteArray", new object[0]);
			using (SHA1Managed sHA1Managed = new SHA1Managed())
			{
				byte[] inArray = sHA1Managed.ComputeHash(buffer);
				result.SignatureHash = Convert.ToBase64String(inArray);
			}
		}
		catch (Exception arg)
		{
			string message = string.Format("Error while retrieving Android package info:    {0}", arg);
			BCWDebug.LogError(message);
		}
		return result;
	}

	private void CheckActualSignature(string actualSignatureHash)
	{
		if (string.IsNullOrEmpty(intendedSignatureHash))
		{
			BCWDebug.LogError("intendedSignatureHash == null");
		}
		else if (string.Compare(actualSignatureHash, intendedSignatureHash, StringComparison.Ordinal) != 0)
		{
			BCWDebug.LogError("Verification FakeSignatureDetected:    " + actualSignatureHash);
			Application.Quit();
		}
		else
		{
			BCWDebug.Log("Signature check passed.");
		}
	}

	private void CheckActualPackageName()
	{
		string actualAndroidPackageName = GetActualAndroidPackageName();
		if (!string.IsNullOrEmpty(actualAndroidPackageName) && string.Compare(actualAndroidPackageName, settings.GetPackageNameByStore(settings.typeStore.PlayStore), StringComparison.Ordinal) != 0)
		{
			BCWDebug.LogWarning("Current shop" + settings.typeStore.PlayStore);
			BCWDebug.LogWarning("Verification FakeBundleDetected: " + actualAndroidPackageName);
			Application.Quit();
		}
	}

	private string GetActualAndroidPackageName()
	{
		string result = string.Empty;
		try
		{
			AndroidJavaObject @static = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
			result = @static.Call<string>("getPackageName", new object[0]);
		}
		catch (Exception message)
		{
			BCWDebug.LogWarning(message);
		}
		return result;
	}
}
